package com.scenic.userservice.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scenic.common.bean.StrategyGuide;
import com.scenic.common.exception.ScenicSpotException;
import com.scenic.common.service.StrategyGuideService;
import com.scenic.userservice.mapper.StrategyGuideMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class StrategyGuideServiceImpl implements StrategyGuideService {
    @Autowired
    private StrategyGuideMapper strategyGuideMapper;
    @Override
    public Long insert(StrategyGuide strategyGuide) {
        strategyGuideMapper.insert(strategyGuide);
        return strategyGuide.getId();
    }

    @Override
    public boolean delete(Long id, Integer userId) throws ScenicSpotException {
        //判断这个攻略是否存在
        StrategyGuide strategyGuide = strategyGuideMapper.selectById(id);
        if (strategyGuide == null) {
            throw new ScenicSpotException("该攻略不存在，无法删除");
        }
        return strategyGuideMapper.delete(id) ==1;
    }

    @Override
    public boolean update(StrategyGuide strategyGuide, Integer userId) throws ScenicSpotException {
        //判断这个攻略是否存在
        StrategyGuide a = strategyGuideMapper.selectById(strategyGuide.getId());
        if (a == null) {
            throw new ScenicSpotException("该攻略不存在，无法删除");
        }
        return strategyGuideMapper.update(strategyGuide) ==1;
    }

    @Override
    public List<StrategyGuide> selectByCondition(StrategyGuide condition) {
        return strategyGuideMapper.selectByCondition(condition);
    }

    @Override
    public PageInfo<StrategyGuide> selectByPage(Integer pageNum, Integer pageSize, StrategyGuide condition) {
        PageHelper.startPage(pageNum, pageSize);
        List<StrategyGuide> list = strategyGuideMapper.selectByCondition(condition);
        PageInfo<StrategyGuide> PageInfo = new PageInfo<>(list);
        return PageInfo;
    }

    @Override
    public StrategyGuide selectById(Long id) {
        return strategyGuideMapper.selectById(id);
    }
}
